﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;


public partial class Main_Inventory_PurInvoiceWithBillLevel : System.Web.UI.Page
{
    Datautility DU = new Datautility();

    string strsql = "";
    int tblItemsCounter = 0;//Declare Counter for - No discount and tax at bill level
   
    
    protected void Page_Load(object sender, EventArgs e)
    {       
        if(!IsPostBack)
        {            
           
            if (Request.QueryString["Poid"] != null)
            {
                hyperPoNumber.Attributes.Add("onclick", "OpenWindow(" + Request.QueryString["Poid"].ToString() + ");");
                BindPurchaseOrderDetails();
            }
            //*****Common Function*******
            txtDate.Text = string.Format("{0:dd/MM/yyyy}", Convert.ToDateTime(System.DateTime.Now)).ToString();
            
            Bind_DDLTaxGroup();
            BindPurchaseLedger();
            BindTaxLedger();
            Bind_VoucherNo();
            Enable_TextBoxs();
            //lblMsg.Text = InsertVoucherDetails();
        }
        //*********Bind Purchase Order Items***********
        BindPOTaxBillLevel();
       
    }
    protected void AddMoreTblBillLevel()
    {
        tblItemsCounter++;

        HtmlTableRow HTTblItemRow = new HtmlTableRow();

        HtmlTableCell HTTblItemCell1 = new HtmlTableCell();
        Label lblItemName = new Label();
        lblItemName.ID = "lblItemName" + tblItemsCounter.ToString();
        lblItemName.Width = Unit.Percentage(99);
        lblItemName.CssClass = "labelText";
        lblItemName.BackColor = Color.FromName("#EAF2F2");
        lblItemName.ToolTip = "Not Changable";
        HiddenField hidItemId = new HiddenField();
        hidItemId.ID = "hidItemId" + tblItemsCounter.ToString();
        HTTblItemCell1.Controls.Add(lblItemName);
        HTTblItemCell1.Controls.Add(hidItemId);



        HtmlTableCell HTTblItemCell2 = new HtmlTableCell();
        Label lblUnitName = new Label();
        lblUnitName.ID = "lblUnitName" + tblItemsCounter.ToString();
        lblUnitName.Width = Unit.Percentage(99);
        lblUnitName.CssClass = "labelText";
        lblUnitName.BackColor = Color.FromName("#EAF2F2");
        HiddenField hidUnitId = new HiddenField();
        hidUnitId.ID = "hidUnitId" + tblItemsCounter.ToString();
        HTTblItemCell2.Controls.Add(lblUnitName);
        HTTblItemCell2.Controls.Add(hidUnitId);



        HtmlTableCell HTTblItemCell3 = new HtmlTableCell();
        TextBox txtReceivedQty = new TextBox();
        txtReceivedQty.ID = "txtReceivedQty" + tblItemsCounter.ToString();
        txtReceivedQty.Width = Unit.Percentage(99);
        txtReceivedQty.CssClass = "inputnumber";
        txtReceivedQty.Text = "0.00";
        txtReceivedQty.BackColor = Color.FromName("#EAF2F2");
        txtReceivedQty.Attributes.Add("onkeydown", "return CatchTab(this.id,event);");
        txtReceivedQty.Attributes.Add("onpaste", "return false");
        txtReceivedQty.Attributes.Add("oncut", "return false");        
        //txtReceivedQty.Attributes.Add("onclick", "return RemoveZero(this.id);");
        //txtReceivedQty.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event)");
        //txtReceivedQty.Attributes.Add("onblur", "return CalculateAmount();");
        txtReceivedQty.AutoCompleteType = AutoCompleteType.Disabled;
        HiddenField hidOrderQty = new HiddenField();
        hidOrderQty.ID = "hidOrderQty" + tblItemsCounter.ToString();
        HTTblItemCell3.Controls.Add(txtReceivedQty);        
        HTTblItemCell3.Controls.Add(hidOrderQty); 
 
        HtmlTableCell HTTblItemCell4 = new HtmlTableCell();
        Button btnQuantity = new Button();
        btnQuantity.ID = "btnQuantity" + tblItemsCounter.ToString();
        btnQuantity.Text = "...";
        btnQuantity.CssClass = "button";
        btnQuantity.Width = Unit.Percentage(99);
        HiddenField hidLocationId = new HiddenField();
        hidLocationId.ID = "hidLocationId" + tblItemsCounter.ToString();
        HTTblItemCell4.Controls.Add(btnQuantity);
        HTTblItemCell4.Controls.Add(hidLocationId);
        
             

        HtmlTableCell HTTblItemCell5 = new HtmlTableCell();
        TextBox txtRate = new TextBox();
        txtRate.ID = "txtRate" + tblItemsCounter.ToString();
        txtRate.Width = Unit.Percentage(99);
        txtRate.CssClass = "inputnumber";
        txtRate.Text = "0.00";
        txtRate.Attributes.Add("onfocus", "return RemoveZero(this.id);");
        txtRate.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event)");
        txtRate.Attributes.Add("onblur", "return CalculateAmount();");
        txtRate.AutoCompleteType = AutoCompleteType.Disabled;
        HiddenField hidPoRate = new HiddenField();
        hidPoRate.ID = "hidPoRate" + tblItemsCounter.ToString();
        HTTblItemCell5.Controls.Add(txtRate);
        HTTblItemCell5.Controls.Add(hidPoRate);



        HtmlTableCell HTTblItemCell6 = new HtmlTableCell();
        TextBox txtGrossAmt = new TextBox();
        txtGrossAmt.ID = "txtGrossAmt" + tblItemsCounter.ToString();
        txtGrossAmt.Width = Unit.Percentage(99);
        txtGrossAmt.CssClass = "inputnumber";
        txtGrossAmt.BackColor = Color.FromName("#EAF2F2");
        txtGrossAmt.ToolTip = "Gross Amount";
        txtGrossAmt.Attributes.Add("onkeydown", "return CatchTab(this.id,event);");
        txtGrossAmt.Attributes.Add("onpaste", "return false");
        txtGrossAmt.Attributes.Add("oncut", "return false");
        txtGrossAmt.AutoCompleteType = AutoCompleteType.Disabled;
        HiddenField hidTaxAmt = new HiddenField();
        hidTaxAmt.ID = "hidTaxAmt" + tblItemsCounter.ToString();
        HTTblItemCell6.Controls.Add(txtGrossAmt);
        HTTblItemCell6.Controls.Add(hidTaxAmt);


        HtmlTableCell HTTblItemCell7 = new HtmlTableCell();
        TextBox txtAmountPayable = new TextBox();
        txtAmountPayable.ID = "txtAmountPayable" + tblItemsCounter.ToString();
        txtAmountPayable.Width = Unit.Percentage(99);
        txtAmountPayable.CssClass = "inputnumber";
        txtAmountPayable.BackColor = Color.FromName("#EAF2F2");
        txtAmountPayable.ToolTip = "Net Amount";
        txtAmountPayable.Attributes.Add("onkeydown", "return CatchTab(this.id,event);");
        txtAmountPayable.Attributes.Add("onpaste", "return false");
        txtAmountPayable.Attributes.Add("oncut", "return false");
        txtAmountPayable.AutoCompleteType = AutoCompleteType.Disabled;
        HTTblItemCell7.Controls.Add(txtAmountPayable);


        HTTblItemRow.Cells.Add(HTTblItemCell1);
        HTTblItemRow.Cells.Add(HTTblItemCell2);
        HTTblItemRow.Cells.Add(HTTblItemCell3);
        HTTblItemRow.Cells.Add(HTTblItemCell4);
        HTTblItemRow.Cells.Add(HTTblItemCell5);
        HTTblItemRow.Cells.Add(HTTblItemCell6);       
        HTTblItemRow.Cells.Add(HTTblItemCell7);       
        tblTaxBillLevel.Rows.Add(HTTblItemRow);


        hidControlTblItems.Value = tblItemsCounter.ToString();

    }
   
    protected void Bind_DDLTaxGroup()//bind tax group
    {
        DataTable DT = DU.getdatausingprocedure("USP_ACC_GetPurchaseMainClass");
        if (DT.Rows.Count > 0)
        {
            ddlTaxGroup.DataSource = DT;
            ddlTaxGroup.DataTextField = "MAINCLASSNAME";
            ddlTaxGroup.DataValueField = "MAINCLASSID";
            ddlTaxGroup.DataBind();
            ddlTaxGroup.Items.Insert(0, new ListItem("", "0"));
            ddlTaxGroup.SelectedValue = "0";

        }
    }
    protected void BindPurchaseLedger()//bind purchase ledger 
    {

        DataTable DT = DU.getdatausingprocedure("USP_IVR_GetPurchaseLedgerforPurchaseInvoice");
        if (DT.Rows.Count > 0)
        {
            ddlPurchaseLedger.DataSource = DT;
            ddlPurchaseLedger.DataTextField = "LNAME";
            ddlPurchaseLedger.DataValueField = "LID";
            ddlPurchaseLedger.DataBind();
            ddlPurchaseLedger.Items.Insert(0, new ListItem("- Select Ledger -", "0"));
        }
        else
        {
            ddlPurchaseLedger.Items.Clear();
        }
    }
    protected void BindTaxLedger()//bind Tax ledger 
    {

        DataTable DT = DU.getdatausingprocedure("USP_IVR_GetTaxLedgerforPurchaseInvoice");
        if (DT.Rows.Count > 0)
        {
            ddlTaxLedger.DataSource = DT;
            ddlTaxLedger.DataTextField = "Lname";
            ddlTaxLedger.DataValueField = "LID";
            ddlTaxLedger.DataBind();
            ddlTaxLedger.Items.Insert(0, new ListItem("- Select Ledger -", "0"));
        }
        else
        {
            ddlTaxLedger.Items.Clear();
        }
    }   
   
   
    protected void ddlTaxGroup_SelectedIndexChanged(object sender, EventArgs e)
    {
        hidTaxRate.Value = "";
        ddlTaxLedger.SelectedIndex = 0;
        reqTaxLedger.Enabled = false;
        ddlTaxLedger.Enabled = false;  
        ResetTaxAmount();
        BindTaxSubGroup(ddlVat);        
        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "CalculateAmount();", true);
    }

    protected void ResetTaxAmount()//a common function reset tax Amount on hiddenfield
    {
        for (int i = 1; i < tblTaxBillLevel.Rows.Count; i++)
        {
            HiddenField hidTaxAmt1 = (HiddenField)tblTaxBillLevel.Rows[i].FindControl("hidTaxAmt" + i.ToString());
            hidTaxAmt1.Value = "";
        }
    }
    
    protected void BindPurchaseOrderDetails()
    {
        SqlParameter[] sparam = {
                                    new SqlParameter("@POID",Request.QueryString["Poid"].ToString())
                                };
        DataTable DTPoDetails = DU.getdatausingprocedure("USP_IVR_GetPoDetailsForPurchaseInvoice", sparam);
        if (DTPoDetails.Rows.Count > 0)
        {
            lblPONumber.Text = DTPoDetails.Rows[0]["PoVoucherNo"].ToString();
            lblSupplier.Text = DTPoDetails.Rows[0]["LNAME"].ToString();
            hidSupplierID.Value = DTPoDetails.Rows[0]["LID"].ToString();
        }
    }
   
    protected void BindPOTaxBillLevel()
    {
        
        SqlParameter[] sparam = {
                                    new SqlParameter("@POID",Request.QueryString["Poid"].ToString())
                                };
        DataTable DTPoDetails = DU.getdatausingprocedure("USP_IVR_GetPoDetailsForPurchaseInvoice", sparam);
        if (DTPoDetails.Rows.Count > 0)
        {
            int rowCount = DTPoDetails.Rows.Count;
            if (rowCount > 0)
            {
                for (int i = 1; i <= rowCount; i++)
                {
                    AddMoreTblBillLevel();
                }
            }           
            for (int i = 1; i <= rowCount; i++)
            {

                Label lblItemName = (Label)tblTaxBillLevel.Rows[i].FindControl("lblItemName" + i.ToString());
                HiddenField hidItemId = (HiddenField)tblTaxBillLevel.Rows[i].FindControl("hidItemId" + i.ToString());
                Label lblUnitName = (Label)tblTaxBillLevel.Rows[i].FindControl("lblUnitName" + i.ToString());
                HiddenField hidUnitId = (HiddenField)tblTaxBillLevel.Rows[i].FindControl("hidUnitId" + i.ToString());
                HiddenField hidOrderQty = (HiddenField)tblTaxBillLevel.Rows[i].FindControl("hidOrderQty" + i.ToString());
                HiddenField hidPoRate = (HiddenField)tblTaxBillLevel.Rows[i].FindControl("hidPoRate" + i.ToString());
                TextBox txtReceivedQty = (TextBox)tblTaxBillLevel.Rows[i].FindControl("txtReceivedQty" + i.ToString());
                Button btnQuantity = (Button)tblTaxBillLevel.Rows[i].FindControl("btnQuantity" + i.ToString());
                HiddenField hidLocationId = (HiddenField)tblTaxBillLevel.Rows[i].FindControl("hidLocationId" + i.ToString());
                
                lblItemName.Text = DTPoDetails.Rows[i - 1]["ITEMNAME"].ToString();
                hidItemId.Value = DTPoDetails.Rows[i - 1]["ItemId"].ToString();
                lblUnitName.Text = DTPoDetails.Rows[i - 1]["UOMSYMBOL"].ToString();
                hidUnitId.Value = DTPoDetails.Rows[i - 1]["UOMID"].ToString();
                hidOrderQty.Value = DTPoDetails.Rows[i - 1]["ORDERQTY"].ToString();
                hidPoRate.Value = DTPoDetails.Rows[i - 1]["PORATE"].ToString();
                lblUnitName.ToolTip = DTPoDetails.Rows[i - 1]["UOMNAME"].ToString();
                txtReceivedQty.ToolTip = "Ordered Qty.  ( " + string.Format("{0:0.00}", Convert.ToDouble(hidOrderQty.Value)) + " )";
                btnQuantity.Attributes.Add("onclick", "return GetQuantity('" + hidItemId.Value.ToString() + "','" + txtReceivedQty.ClientID.ToString() + "','" + hidOrderQty.Value.ToString() + "','" + hidLocationId.ClientID.ToString() + "');");
                
            }
        }
    }


    protected void BindTaxSubGroup(DropDownList ddlTaxType)//a common function for bind tax type dropdown list according tax group
    {
        if (ddlTaxGroup.SelectedIndex > 0)
        {
            strsql = "select SUBCLASSID,SUBCLASSNAME from UVW_ACC_TaxMaster where mainclassid='" + ddlTaxGroup.SelectedValue + "'";
            DataTable DT = DU.getdata(strsql);
            if (DT.Rows.Count > 0)
            {
                ddlTaxType.DataSource = DT;
                ddlTaxType.DataTextField = "SUBCLASSNAME";
                ddlTaxType.DataValueField = "SUBCLASSID";
                ddlTaxType.DataBind();
                ddlTaxType.Items.Insert(0, new ListItem(" ", "0"));
            }
        }
        else
        {
            ddlTaxType.Items.Clear();
        }
    }
    protected void ddlVat_SelectedIndexChanged(object sender, EventArgs e)
    {        
        if (Convert.ToDouble(ddlVat.SelectedValue) > 0)
        {
            strsql = "select DEFTAXRATE from UVW_ACC_TaxMaster where mainclassid='" + ddlTaxGroup.SelectedValue + "' and subclassid='" + ddlVat.SelectedValue + "'";
            DataTable DT = DU.getdata(strsql);
            if (DT.Rows.Count > 0)
            {
                
                if (DT.Rows[0]["DEFTAXRATE"].ToString() != string.Empty)
                {
                    double TaxRate = Convert.ToDouble(DT.Rows[0]["DEFTAXRATE"].ToString());
                    hidTaxRate.Value = TaxRate.ToString();
                    ddlTaxLedger.Enabled = true;
                    reqTaxLedger.Enabled = true;
                    
                }
                else
                {
                    ddlTaxLedger.SelectedIndex = 0;
                    reqTaxLedger.Enabled = false;
                    ddlTaxLedger.Enabled = false;                      
                    hidTaxRate.Value = "";
                }
            }
            else
            {
                ddlTaxLedger.SelectedIndex = 0;
                reqTaxLedger.Enabled = false;
                ddlTaxLedger.Enabled = false;
                hidTaxRate.Value = "";
            }
        }
        else
        {
            ddlTaxLedger.SelectedIndex = 0;
            reqTaxLedger.Enabled = false;
            ddlTaxLedger.Enabled = false;
            ddlVat.SelectedValue = "0";
            hidTaxRate.Value = "";
        }
        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "CalculateAmount();", true);
    }

    protected void Bind_VoucherNo()
    {
        string MainVoucher = "";
        string Prepix = "";
        string Suffix = "";
        strsql = "Select tfoid from ivr_tranformat where trantype=4";
        DataTable DTTfoid = DU.getdata(strsql);
        if (DTTfoid.Rows.Count > 0)
        {
            hidBillformatId.Value = DTTfoid.Rows[0]["tfoid"].ToString();
            strsql = "select max(convert(int,TRNNUM)) as PoVoucherNo from IVR_TRAN_MAIN where TFOID='" + DTTfoid .Rows[0]["tfoid"].ToString()+ "' and trantypeid=4";
            DataTable DTNum = DU.getdata(strsql);
            if (DTNum.Rows.Count > 0 && DTNum.Rows[0][0].ToString() != string.Empty)
            {
                strsql = "Select * from IVR_TRANFORMATNUM where TFOID='" + DTTfoid.Rows[0]["tfoid"].ToString() + "'";
                DataTable DT = DU.getdata(strsql);
                if (DT.Rows.Count > 0)
                {
                    if (DT.Rows[0]["NUMBERINGMETHOD"].ToString() == "True")
                    {
                        string SetZero = "";
                        int NumLength = Convert.ToInt32(DT.Rows[0]["NUMSTART"].ToString().Length);
                        int NumWidth = Convert.ToInt32(DT.Rows[0]["NUMDIGITS"].ToString());
                        int Diff = NumWidth - NumLength;
                        if (NumLength < NumWidth)
                        {
                            for (int i = 0; i < Diff; i++)
                            {
                                SetZero += "0";
                            }

                        }
                        double Povoucher = Convert.ToDouble(DTNum.Rows[0]["PoVoucherNo"].ToString());
                        MainVoucher = SetZero + (Povoucher + 1).ToString();
                        txtVoucher.Attributes.Add("onKeyPress", "return false");
                        txtVoucher.Attributes.Add("onKeyDown", "return false");
                    }
                    else
                    {
                        txtVoucher.Attributes.Add("onKeyPress", "return true");
                        txtVoucher.Attributes.Add("onKeyDown", "return true");
                    }
                    Prepix = DT.Rows[0]["NUMPREFIX"].ToString();
                    Suffix = DT.Rows[0]["NUMSUFFIX"].ToString();

                }
            }
            else
            {
                strsql = "Select * from IVR_TRANFORMATNUM where TFOID='" + DTTfoid.Rows[0]["tfoid"].ToString() + "'";
                DataTable DT = DU.getdata(strsql);
                if (DT.Rows.Count > 0)
                {
                    if (DT.Rows[0]["NUMBERINGMETHOD"].ToString() == "True")
                    {
                        string SetZero = "";
                        int NumLength = Convert.ToInt32(DT.Rows[0]["NUMSTART"].ToString().Length);
                        int NumWidth = Convert.ToInt32(DT.Rows[0]["NUMDIGITS"].ToString());
                        int Diff = NumWidth - NumLength;
                        if (NumLength < NumWidth)
                        {
                            for (int i = 0; i < Diff; i++)
                            {
                                SetZero += "0";
                            }

                        }
                        MainVoucher = SetZero + DT.Rows[0]["NUMSTART"].ToString();
                        txtVoucher.Attributes.Add("onKeyPress", "return false");
                        txtVoucher.Attributes.Add("onKeyDown", "return false");
                    }
                    else
                    {
                        txtVoucher.Attributes.Add("onKeyPress", "return true");
                        txtVoucher.Attributes.Add("onKeyDown", "return true");
                    }
                    Prepix = DT.Rows[0]["NUMPREFIX"].ToString();
                    Suffix = DT.Rows[0]["NUMSUFFIX"].ToString();
                }

            }
            txtPrefix.Text = Prepix.ToString();
            txtVoucher.Text = MainVoucher.ToString();
            txtSuffix.Text = Suffix.ToString();
        }
    }
    protected void Enable_TextBoxs()
    {
        strsql = "Select * from IVR_TRANFORMAT where trantype=4";
        DataTable DT = DU.getdata(strsql);
        if (DT.Rows.Count > 0)
        {
            if (DT.Rows[0]["PREPAREDBY"].ToString() == "True")
            {
                txtPreparedBy.Enabled = true;
                txtPreparedBy.BackColor = Color.White;
            }
            else
            {
                txtPreparedBy.Enabled = false;
                txtPreparedBy.BackColor = ColorTranslator.FromHtml("#d4d0c8");
            }
            if (DT.Rows[0]["CHECKEDBY"].ToString() == "True")
            {
                txtCheckedBy.Enabled = true;
                txtCheckedBy.BackColor = Color.White;
            }
            else
            {
                txtCheckedBy.Enabled = false;
                txtCheckedBy.BackColor = ColorTranslator.FromHtml("#d4d0c8");
            }
            if (DT.Rows[0]["AUTHSIGNATORY"].ToString() == "True")
            {
                txtSignatory.Enabled = true;
                txtSignatory.BackColor = Color.White;
            }
            else
            {
                txtSignatory.Enabled = false;
                txtSignatory.BackColor = ColorTranslator.FromHtml("#d4d0c8");
            }
            if (DT.Rows[0]["NARRATION"].ToString() == "True")
            {
                txtNarration.Enabled = true;
                txtNarration.BackColor = Color.White;
            }
            else
            {
                txtNarration.Enabled = false;
                txtNarration.BackColor = ColorTranslator.FromHtml("#d4d0c8");
            }
            if (DT.Rows[0]["REMARKS"].ToString() == "True")
            {
                txtRemark.Enabled = true;
                txtRemark.BackColor = Color.White;
            }
            else
            {
                txtRemark.Enabled = false;
                txtRemark.BackColor = ColorTranslator.FromHtml("#d4d0c8");
            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)    
    {        
        InsertItemTranMain(InsertVouchers());       
    }

    protected string InsertVouchers()
    {
        string strTransactionTypeID = string.Empty;
        string strFinYear = string.Empty;
        string strTransDate = string.Empty;
        string strRefId = string.Empty;
        int intResult = 0;
        try
        {
            SqlParameter[] sparam = {
                                    new SqlParameter("@PARENTTYPEID","7"),
                                    new SqlParameter("@BILLFORMATID",hidBillformatId.Value.ToString()),
                                    new SqlParameter("@MODULETYPE","2")
                                };
            DataTable DTRef = DU.getdatausingprocedure("USP_ACC_GetTransactionSettingBasedOnFormat", sparam);
            if (DTRef.Rows.Count > 0)
            {
                strTransactionTypeID = DTRef.Rows[0]["TYPEID"].ToString();
            }
            
            strsql = "Select * from MAS_FINYEAR";
            DataTable DTFinYear = DU.getdata(strsql);
            if (DTFinYear.Rows.Count > 0)
            {
                strFinYear = DTFinYear.Rows[0]["FINYEAR"].ToString();
            }
            
            if (txtDate.Text.ToString() != string.Empty)
            {
                string[] str = txtDate.Text.ToString().Split('/');               
                strTransDate = str[1].ToString() + "/" + str[0].ToString() + "/" + str[2].ToString();
            }            
            if (strTransactionTypeID.ToString() != string.Empty)
            {
                SqlParameter[] sparam1 = { 
                                    new SqlParameter("@TransactionTypeID",strTransactionTypeID.ToString()),
                                    new SqlParameter("@TransactionNumber",txtVoucher.Text.Trim()),
                                    new SqlParameter("@FinYear",strFinYear.ToString()),
                                    new SqlParameter("@ReferenceNumber",txtBillRef1.Text.Trim()),
                                    new SqlParameter("@IsCashVoucher","0"),
                                    new SqlParameter("@TransactionDate",strTransDate.ToString()),
                                    new SqlParameter("@CreditDays",txtCreditDay.Text.Trim()),
                                    new SqlParameter("@CreatedBy",Session["username"].ToString()),
                                    new SqlParameter("@IsCancelled","0"),
                                    new SqlParameter("@CostCentreClassID","0")
                                };
                intResult = DU.executeprocedure("USP_ACC_SaveTransaction", sparam1);

                if (intResult > 0)
                {
                    strsql = "Select Ident_Current('ACC_VOUCHERS') as TranId";
                    DataTable DTTranid = DU.getdata(strsql);
                    if (DTTranid.Rows.Count > 0)
                    {
                        strRefId = DTTranid.Rows[0]["TranId"].ToString();
                        InsertVoucherDetails(DTTranid.Rows[0]["TranId"].ToString());
                    }
                }
            }            
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message.ToString();
        }

        return strRefId.ToString();       
    }

    protected void InsertVoucherDetails(string TranId)
    {
        if (TranId.ToString() != string.Empty)
        {
            if (hidSupplierID.Value.ToString() != string.Empty)
            {
                decimal Amount = 0;
                if (txtGrandTotal.Text != string.Empty)
                {
                    Amount = Convert.ToDecimal(txtGrandTotal.Text.Trim());
                }
                SqlParameter[] sparam = {
                                    new SqlParameter("@TransactionID",TranId.ToString()),
                                    new SqlParameter("@LedgerID",hidSupplierID.Value.ToString()),
                                    new SqlParameter("@Amount",Amount.ToString()),
                                    new SqlParameter("@IsReconciled","0")
                                };
                int result = DU.executeprocedure("USP_ACC_SaveTransactionDetails", sparam);
                if (result > 0)
                {
                    strsql = "Select Ident_Current('ACC_VOUDETAILS') as VouSerial";
                    DataTable DTVouSerial = DU.getdata(strsql);
                    if (DTVouSerial.Rows.Count > 0)
                    {
                        InsertReFDetails(DTVouSerial.Rows[0]["VouSerial"].ToString());
                    }
                }
            }
            if (ddlPurchaseLedger.SelectedValue!="0")
            {
                decimal Amount = 0;
                if (txtTaxableAmt.Text != string.Empty)
                {
                    decimal Amt = Convert.ToDecimal(txtTaxableAmt.Text.Trim());
                    Amount = 0 - Amt;
                }
                SqlParameter[] sparam = {
                                    new SqlParameter("@TransactionID",TranId.ToString()),
                                    new SqlParameter("@LedgerID",ddlPurchaseLedger.SelectedValue),
                                    new SqlParameter("@Amount",Amount.ToString()),
                                    new SqlParameter("@IsReconciled","0")
                                };
                DU.executeprocedure("USP_ACC_SaveTransactionDetails", sparam);
            }
            if (ddlTaxLedger.SelectedValue!="0")
            {
                decimal Amount = 0;
                if (txtVat.Text != string.Empty)
                {
                    decimal Amt = Convert.ToDecimal(txtVat.Text.Trim());
                    Amount = 0 - Amt;
                }
                SqlParameter[] sparam = {
                                    new SqlParameter("@TransactionID",TranId.ToString()),
                                    new SqlParameter("@LedgerID",ddlTaxLedger.SelectedValue),
                                    new SqlParameter("@Amount",Amount.ToString()),
                                    new SqlParameter("@IsReconciled","0")
                                };
                DU.executeprocedure("USP_ACC_SaveTransactionDetails", sparam);
            }
        }

    }
    protected void InsertReFDetails(string strVouSerial)
    {
        if (strVouSerial != string.Empty)
        {
            decimal Amount = 0;
            string strRefDate = string.Empty;
            string strRefferenceNo = string.Empty;
            if (txtDate.Text.ToString() != string.Empty)
            {
                string[] str = txtDate.Text.ToString().Split('/');
                strRefDate = str[1].ToString() + "/" + str[0].ToString() + "/" + str[2].ToString();
            }
            if (txtGrandTotal.Text != string.Empty)
            {
                Amount = Convert.ToDecimal(txtGrandTotal.Text.Trim());
            }
            strsql = "SELECT FORMATNAME FROM IVR_TRANFORMAT WHERE TFOID='" + hidBillformatId.Value + "'";
            DataTable DTFormatName = DU.getdata(strsql);
            if (DTFormatName.Rows.Count > 0)
            {
                strRefferenceNo += DTFormatName.Rows[0]["FORMATNAME"].ToString() + " ";
                if (txtPrefix.Text != string.Empty)
                {
                    strRefferenceNo += txtPrefix.Text + "/";
                }
                strRefferenceNo += txtVoucher.Text;
                if (txtSuffix.Text != string.Empty)
                {
                    strRefferenceNo += "/" + txtSuffix.Text;
                }

            }
            SqlParameter[] sparam1 = {
                                            new SqlParameter("@ReferenceSerial","0"),
                                            new SqlParameter("@ParentReference","0"),
                                            new SqlParameter("@TransactionSerial",strVouSerial),
                                            new SqlParameter("@ReferenceType","1"),
                                            new SqlParameter("@Reference",strRefferenceNo),
                                            new SqlParameter("@ReferenceDate",strRefDate),
                                            new SqlParameter("@CreditDays",txtCreditDay .Text),
                                            new SqlParameter("@ReferenceAmount",Amount.ToString()),
                                            new SqlParameter("@IsTDSDeducted","0")
                                };
            DU.executeprocedure("USP_ACC_SaveReferenceDetails", sparam1);

        }
    }

    protected void InsertItemTranMain(string strRefId)
    {       
        string strFinYear = string.Empty;
        string strTransDate = string.Empty;
        string strRefDate = "01/01/1900";
        string strEffectiveDate = string.Format("{0:MM/dd/yyyy}", System.DateTime.Now).ToString();       
        strsql = "Select * from MAS_FINYEAR";
        DataTable DTFinYear = DU.getdata(strsql);
        if (DTFinYear.Rows.Count > 0)
        {
            strFinYear = DTFinYear.Rows[0]["FINYEAR"].ToString();
        }
        if (txtDate.Text.ToString() != string.Empty)
        {
            string[] str = txtDate.Text.ToString().Split('/');
            strTransDate = str[1].ToString() + "/" + str[0].ToString() + "/" + str[2].ToString();
        }
        if (txtBillRef2.Text.ToString() != string.Empty)
        {
            string[] str = txtBillRef2.Text.ToString().Split('/');
            strRefDate = str[1].ToString() + "/" + str[0].ToString() + "/" + str[2].ToString();
        }

        if (strRefId.ToString() != string.Empty)
        {
            SqlParameter[] sparam ={
                                   new SqlParameter("@TRANREFERENCEID",strRefId.ToString()),
                                   new SqlParameter("@TRANTYPEID","4"),
                                   new SqlParameter("@FINYEAR",strFinYear.ToString()),
                                   new SqlParameter("@POID",Request.QueryString["Poid"].ToString()),
                                   new SqlParameter("@INVOICENO",txtInvoiceNo.Text.Trim()),
                                   new SqlParameter("@TFOID",hidBillformatId.Value.ToString()),
                                   new SqlParameter("@TRNNUM",txtVoucher.Text.Trim()),
                                   new SqlParameter("@TRNDATE",strTransDate.ToString()),
                                   new SqlParameter("@VENDCUST_LID",hidSupplierID.Value.ToString()),
                                   new SqlParameter("@TAX_MAINCLASSID",ddlTaxGroup.SelectedValue),
                                   new SqlParameter("@TRNSTATUS","1"),
                                   new SqlParameter("@EFFECTIVEDATE",strEffectiveDate.ToString()),
                                   new SqlParameter("@MODEOFPAY",rbtCashCredit.SelectedValue),
                                   new SqlParameter("@CREDITPERIOD",txtCreditDay.Text.Trim()),
                                   new SqlParameter("@REFNUM",txtBillRef1.Text.Trim()),
                                   new SqlParameter("@REFDATE",strRefDate.ToString()),
                                   new SqlParameter("@SURCHARGE","0"),                                   
                                   new SqlParameter("@CNAME",Session["username"].ToString())                                   
                               };
            int intResult = DU.executeprocedure("USP_IVR_SaveTranMain", sparam);
            if (intResult > 0)
            {
                strsql = "Select Ident_Current('IVR_TRAN_MAIN') as TranId";
                DataTable DTTranid = DU.getdata(strsql);
                if (DTTranid.Rows.Count > 0)
                {
                    InsertItemTranItem(DTTranid.Rows[0]["TranId"].ToString());
                    InsertItemTranTotal(DTTranid.Rows[0]["TranId"].ToString());
                    InsertTranDelivery(DTTranid.Rows[0]["TranId"].ToString());
                    InsertTranOthers(DTTranid.Rows[0]["TranId"].ToString());
                }
                lblMsg.Text = "Invoice Created";
            }
        }
    }
    protected void InsertItemTranItem(string strTranId)
    {
        if (strTranId.ToString() != string.Empty)
        {
            int intResult = 0;
            int intCount = Convert.ToInt32(hidControlTblItems.Value);

            for (int i = 1; i <= intCount; i++)
            {
                HiddenField hidItemId = (HiddenField)tblTaxBillLevel.Rows[i].FindControl("hidItemId" + i.ToString());
                HiddenField hidUnitId = (HiddenField)tblTaxBillLevel.Rows[i].FindControl("hidUnitId" + i.ToString());
                TextBox txtReceivedQty = (TextBox)tblTaxBillLevel.Rows[i].FindControl("txtReceivedQty" + i.ToString());
                HiddenField hidTaxAmt = (HiddenField)tblTaxBillLevel.Rows[i].FindControl("hidTaxAmt" + i.ToString());
                TextBox txtRate = (TextBox)tblTaxBillLevel.Rows[i].FindControl("txtRate" + i.ToString());
                TextBox txtAmountPayable = (TextBox)tblTaxBillLevel.Rows[i].FindControl("txtAmountPayable" + i.ToString());
                TextBox txtGrossAmt = (TextBox)tblTaxBillLevel.Rows[i].FindControl("txtGrossAmt" + i.ToString());
                if (hidTaxRate.Value.ToString() == string.Empty)
                {
                    hidTaxRate.Value = "0";
                }
                if (Convert.ToDouble(txtRate.Text) > 0 && Convert.ToDouble(txtReceivedQty.Text) > 0)
                {

                    SqlParameter[] sparam ={
                                   new SqlParameter("@TRNID",strTranId.ToString()),
                                   new SqlParameter("@ITMID",hidItemId.Value.ToString()),
                                   new SqlParameter("@ITMQTY",txtReceivedQty.Text.Trim()),
                                   new SqlParameter("@UOMID",hidUnitId.Value.ToString()),
                                   new SqlParameter("@RATE",txtRate.Text.Trim()),
                                   new SqlParameter("@GROSSAMT",txtAmountPayable.Text.Trim()),
                                   new SqlParameter("@NETAMT",txtGrossAmt.Text.Trim()),
                                   new SqlParameter("@TAXPER",hidTaxRate.Value.ToString()),
                                   new SqlParameter("@TAXAMT",hidTaxAmt.Value.ToString()),
                                   new SqlParameter("@ITMAMT",txtAmountPayable.Text.Trim())                                                                      
                               };
                    intResult = DU.executeprocedure("USP_IVR_SaveTranItem", sparam);
                }
                if (intResult > 0)
                {
                    strsql = "Select Ident_Current('IVR_TRAN_ITEM') as TITID";
                    DataTable DTTranid = DU.getdata(strsql);
                    if (DTTranid.Rows.Count > 0)
                    {
                        InsertItemTranDetails(strTranId,DTTranid.Rows[0]["TITID"].ToString(),i);
                    }
                    intResult = 0;
                }
            }
        }
    }
    protected void InsertItemTranTotal(string strTranId)
    {
        if (strTranId.ToString() != string.Empty)
        {
            int intResult = 0; 
            SqlParameter[] sparam ={
                           new SqlParameter("@TRNID",strTranId.ToString()),
                           new SqlParameter("@ITEMTOTAL",Convert.ToDecimal(txtGrossAmount.Text)),
                           new SqlParameter("@ITEMTOTAL_LID",ddlPurchaseLedger.SelectedValue),
                           new SqlParameter("@TAXABLEAMT",Convert.ToDecimal(txtTaxableAmt.Text)),
                           new SqlParameter("@TAX_TXMASID",ddlVat.SelectedValue),
                           new SqlParameter("@TAXPER",hidTaxRate.Value.ToString()),
                           new SqlParameter("@TAXAMT",Convert.ToDecimal(txtVat.Text)),
                           new SqlParameter("@TAXAMT_LID",ddlTaxLedger.SelectedValue),
                           new SqlParameter("@RNDOFFAMT",Convert.ToDecimal(txtRoundoff.Text)),
                           //new SqlParameter("@RNDOFFAMT_LID",ddlRoundOff.SelectedValue),
                           new SqlParameter("@TRNTOTAL",Convert.ToDecimal(txtGrandTotal.Text))                                                                                                                 
                       };
            intResult = DU.executeprocedure("USP_IVR_SaveTranTotal", sparam);
        }
    }
    protected bool isBatchEnabled(string ItemId)
    {
        bool Flag = true;
        if (ItemId.ToString() != string.Empty)
        {
            strsql = "Select ITMBATCH from IVR_ITEM Where ITMID='" + ItemId.ToString() + "'";
            DataTable DT = DU.getdata(strsql);
            if (DT.Rows.Count > 0)
            {
                if (Convert.ToDouble(DT.Rows[0]["ITMBATCH"]) > 0)
                {
                    Flag = true;
                }
                else
                {
                    Flag = false;
                }
            }
        }
        return Flag;
    }    
    protected bool isWarrantyEnabled(string ItemId)
    {
        bool Flag = true;
        if (ItemId.ToString() != string.Empty)
        {
            strsql = "Select ITMBATCH, ITMBATCHOPT from IVR_ITEM Where ITMID='" + ItemId.ToString() + "'";
            DataTable DT = DU.getdata(strsql);
            if (DT.Rows.Count > 0)
            {
                if (Convert.ToDouble(DT.Rows[0]["ITMBATCH"]) > 0 && Convert.ToDouble(DT.Rows[0]["ITMBATCHOPT"]) == 1)
                {
                    Flag = true;                    
                }
                else
                {
                    Flag = false;                   
                }
            }
        }
        return Flag;
    }
    protected void InsertItemTranDetails(string tranId, string tranItemId, int rowId)
    {
        if (tranId.ToString() != string.Empty && tranItemId.ToString() != string.Empty)
        {
            string strTransDate = string.Empty;
            if (txtDate.Text.ToString() != string.Empty)
            {
                string[] str = txtDate.Text.ToString().Split('/');
                strTransDate = str[1].ToString() + "/" + str[0].ToString() + "/" + str[2].ToString();
            }
            HiddenField hidItemId = (HiddenField)tblTaxBillLevel.Rows[rowId].FindControl("hidItemId" + rowId.ToString());
            HiddenField hidLocationId = (HiddenField)tblTaxBillLevel.Rows[rowId].FindControl("hidLocationId" + rowId.ToString());
            TextBox txtReceivedQty = (TextBox)tblTaxBillLevel.Rows[rowId].FindControl("txtReceivedQty" + rowId.ToString());
            TextBox txtRate = (TextBox)tblTaxBillLevel.Rows[rowId].FindControl("txtRate" + rowId.ToString());

            if (txtReceivedQty.Text != string.Empty && txtRate.Text != string.Empty)
            {
                if (hidLocationId.Value.ToString() != string.Empty)
                {
                    if (isBatchEnabled(hidItemId.Value) && isWarrantyEnabled(hidItemId.Value))
                    {
                        List<string> lstLocationId = new List<string>();
                        List<string> lstBatchId = new List<string>();
                        List<string> lstLocationQty = new List<string>();
                        List<string> lstWarranty1 = new List<string>();
                        List<string> lstWarrantyDetails1 = new List<string>();
                        if (hidLocationId.Value.ToString().Contains("/") && hidLocationId.Value.ToString().Contains("|") && hidLocationId.Value.ToString().Contains(":"))
                        {
                            string[] strMainArray = hidLocationId.Value.ToString().Split('/');
                            foreach (string strSubArray in strMainArray)
                            {
                                string[] str = strSubArray.ToString().Split(',');
                                lstLocationId.Add(str[0].ToString());
                                string[] str1 = str[1].ToString().Split('|');
                                lstLocationQty.Add(str1[0].ToString());
                                string[] str2 = str1[1].ToString().Split(':');
                                lstBatchId.Add(str2[0].ToString());
                                string[] str3 = str2[1].ToString().Split(';');
                                lstWarranty1.Add(str3[0].ToString());
                                lstWarrantyDetails1.Add(str3[1].ToString());
                            }
                        }
                        else
                        {
                            string[] str = hidLocationId.Value.ToString().Split(',');
                            lstLocationId.Add(str[0].ToString());
                            string[] str1 = str[1].ToString().Split('|');
                            lstLocationQty.Add(str1[0].ToString());
                            string[] str2 = str1[1].ToString().Split(':');
                            lstBatchId.Add(str2[0].ToString());
                            string[] str3 = str2[1].ToString().Split(';');
                            lstWarranty1.Add(str3[0].ToString());
                            lstWarrantyDetails1.Add(str3[1].ToString());
                        }
                        if (lstLocationId != null)
                        {
                            int Count = Convert.ToInt32(lstLocationId.Count);
                            for (int j = 0; j < Count; j++)
                            {
                                SqlParameter[] sparam ={
                                                               new SqlParameter("@TITID",tranItemId.ToString()),
                                                               new SqlParameter("@WHSID",lstLocationId[j].ToString()),
                                                               new SqlParameter("@BATID",lstBatchId[j].ToString()),
                                                               new SqlParameter("@DETQTY",lstLocationQty[j].ToString()),
                                                               new SqlParameter("@WARRANTY",lstWarranty1[j].ToString()),
                                                               new SqlParameter("@WARRANTYPERIOD",lstWarrantyDetails1[j].ToString())                                                               
                                                           };
                                int intResult = DU.executeprocedure("USP_IVR_SaveTransactionItemDetails", sparam);
                                if (intResult > 0)
                                {
                                    strsql = "Select Ident_Current('IVR_TRAN_ITEMDET') as TIDID";
                                    DataTable DTTranTidid = DU.getdata(strsql);
                                    if (DTTranTidid.Rows.Count > 0)
                                    {
                                        SqlParameter[] sparam1 ={
                                                               new SqlParameter("@TYOFENTRY","4"),
                                                               new SqlParameter("@ENTRYID",tranId.ToString()),
                                                               new SqlParameter("@ITMROWID",DTTranTidid.Rows[0]["TIDID"].ToString()),
                                                               new SqlParameter("@ITMID",hidItemId.Value),
                                                               new SqlParameter("@WHID",lstLocationId[j].ToString()),
                                                               new SqlParameter("@BATCHID",lstBatchId[j].ToString()),                                                               
                                                               new SqlParameter("@QTYPRIM",lstLocationQty[j].ToString()),                                                              
                                                               new SqlParameter("@RATE_PRIMUNIT",txtRate.Text.Trim()),                                                             
                                                               new SqlParameter("@ENTRYDATE",strTransDate)
                                                                };
                                        int Result = DU.executeprocedure("USP_IVR_SaveStockDetails", sparam1);
                                        if (Result > 0)
                                        {
                                            intResult = 0;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else if (isBatchEnabled(hidItemId.Value) && !isWarrantyEnabled(hidItemId.Value))
                    {
                        List<string> lstLocationId = new List<string>();
                        List<string> lstBatchId = new List<string>();
                        List<string> lstLocationQty = new List<string>();
                        if (hidLocationId.Value.ToString().Contains("/") && hidLocationId.Value.ToString().Contains("|"))
                        {
                            string[] strMainArray = hidLocationId.Value.ToString().Split('/');
                            foreach (string strSubArray in strMainArray)
                            {
                                string[] str = strSubArray.ToString().Split(',');
                                lstLocationId.Add(str[0].ToString());
                                string[] str1 = str[1].ToString().Split('|');
                                lstLocationQty.Add(str1[0].ToString());
                                lstBatchId.Add(str1[1].ToString());
                            }
                        }
                        else
                        {
                            string[] str = hidLocationId.Value.ToString().Split(',');
                            lstLocationId.Add(str[0].ToString());
                            string[] str1 = str[1].ToString().Split('|');
                            lstLocationQty.Add(str1[0].ToString());
                            lstBatchId.Add(str1[1].ToString());
                        }
                        if (lstLocationId != null)
                        {
                            int Count = Convert.ToInt32(lstLocationId.Count);
                            for (int j = 0; j < Count; j++)
                            {
                                SqlParameter[] sparam ={
                                                               new SqlParameter("@TITID",tranItemId.ToString()),
                                                               new SqlParameter("@WHSID",lstLocationId[j].ToString()),
                                                               new SqlParameter("@BATID",lstBatchId[j].ToString()),
                                                               new SqlParameter("@DETQTY",lstLocationQty[j].ToString()),
                                                               new SqlParameter("@WARRANTY","0"),
                                                               new SqlParameter("@WARRANTYPERIOD","0")                                                               
                                                           };
                                int intResult = DU.executeprocedure("USP_IVR_SaveTransactionItemDetails", sparam);
                                if (intResult > 0)
                                {
                                    strsql = "Select Ident_Current('IVR_TRAN_ITEMDET') as TIDID";
                                    DataTable DTTranTidid = DU.getdata(strsql);
                                    if (DTTranTidid.Rows.Count > 0)
                                    {
                                        SqlParameter[] sparam1 ={
                                                               new SqlParameter("@TYOFENTRY","4"),
                                                               new SqlParameter("@ENTRYID",tranId.ToString()),
                                                               new SqlParameter("@ITMROWID",DTTranTidid.Rows[0]["TIDID"].ToString()),
                                                               new SqlParameter("@ITMID",hidItemId.Value),
                                                               new SqlParameter("@WHID",lstLocationId[j].ToString()),
                                                               new SqlParameter("@BATCHID",lstBatchId[j].ToString()),                                                               
                                                               new SqlParameter("@QTYPRIM",lstLocationQty[j].ToString()),                                                              
                                                               new SqlParameter("@RATE_PRIMUNIT",txtRate.Text.Trim()),                                                             
                                                               new SqlParameter("@ENTRYDATE",strTransDate)
                                                                };
                                        int Result = DU.executeprocedure("USP_IVR_SaveStockDetails", sparam1);
                                        if (Result > 0)
                                        {
                                            intResult = 0;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else if (!isBatchEnabled(hidItemId.Value) && !isWarrantyEnabled(hidItemId.Value))
                    {
                        List<string> lstLocationId = new List<string>();
                        List<string> lstLocationQty = new List<string>();
                        if (hidLocationId.Value.ToString().Contains("/"))
                        {
                            string[] strMainArray = hidLocationId.Value.ToString().Split('/');
                            foreach (string strSubArray in strMainArray)
                            {
                                string[] str = strSubArray.ToString().Split(',');
                                lstLocationId.Add(str[0].ToString());
                                lstLocationQty.Add(str[1].ToString());
                            }
                        }
                        else
                        {
                            string[] str = hidLocationId.Value.ToString().Split(',');
                            lstLocationId.Add(str[0].ToString());
                            lstLocationQty.Add(str[1].ToString());
                        }
                        if (lstLocationId != null)
                        {
                            int Count = Convert.ToInt32(lstLocationId.Count);
                            for (int j = 0; j < Count; j++)
                            {
                                SqlParameter[] sparam ={
                                                               new SqlParameter("@TITID",tranItemId.ToString()),
                                                               new SqlParameter("@WHSID",lstLocationId[j].ToString()),                                                               
                                                               new SqlParameter("@DETQTY",lstLocationQty[j].ToString()),
                                                               new SqlParameter("@WARRANTY","0"),
                                                               new SqlParameter("@WARRANTYPERIOD","0")                                                               
                                                           };
                                int intResult = DU.executeprocedure("USP_IVR_SaveTransactionItemDetails", sparam);
                                if (intResult > 0)
                                {
                                    strsql = "Select Ident_Current('IVR_TRAN_ITEMDET') as TIDID";
                                    DataTable DTTranTidid = DU.getdata(strsql);
                                    if (DTTranTidid.Rows.Count > 0)
                                    {
                                        SqlParameter[] sparam1 ={
                                                               new SqlParameter("@TYOFENTRY","4"),
                                                               new SqlParameter("@ENTRYID",tranId.ToString()),
                                                               new SqlParameter("@ITMROWID",DTTranTidid.Rows[0]["TIDID"].ToString()),
                                                               new SqlParameter("@ITMID",hidItemId.Value),
                                                               new SqlParameter("@WHID",lstLocationId[j].ToString()),
                                                               new SqlParameter("@BATCHID","0"),                                                               
                                                               new SqlParameter("@QTYPRIM",lstLocationQty[j].ToString()),                                                              
                                                               new SqlParameter("@RATE_PRIMUNIT",txtRate.Text.Trim()),                                                             
                                                               new SqlParameter("@ENTRYDATE",strTransDate)
                                                                };
                                        int Result = DU.executeprocedure("USP_IVR_SaveStockDetails", sparam1);
                                        if (Result > 0)
                                        {
                                            intResult = 0;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }    
    protected void InsertTranOthers(string tranId)
    {
        if (tranId.ToString() != string.Empty)
        {
            SqlParameter[] sparam =
                                    {
                                        new SqlParameter("@TRNID",tranId.ToString()),
                                        new SqlParameter("@PREPAREDBY",txtPreparedBy.Text.Trim()),
                                        new SqlParameter("@CHECKEDBY",txtCheckedBy.Text.Trim()),
                                        new SqlParameter("@AUTHRSDSIGNAT",txtSignatory.Text.Trim()),
                                        new SqlParameter("@NARRATION",txtNarration.Text.Trim()),
                                        new SqlParameter("@REMARKS",txtRemark.Text.Trim())                                        
                                    };
            DU.executeprocedure("USP_IVR_SaveTranOthers", sparam);
        }
    }

    protected void InsertTranDelivery(string tranId)
    {
        if (tranId.ToString() != string.Empty)
        {
            string strDeliveryDate = "1/1/1900";
            if (txtDeliveryDate.Text != string.Empty)
            {
                string[] str = txtDeliveryDate.Text.ToString().Split('/');
                strDeliveryDate = str[1] + "/" + str[0] + "/" + str[2];
            }
            SqlParameter[] sparam =
                                    {
                                        new SqlParameter("@TRNID",tranId.ToString()),
                                        new SqlParameter("@MODEOFDELIVERY",txtDeliveryMode.Text.Trim()),
                                        new SqlParameter("@DELIVERYDATE",strDeliveryDate) ,
                                        new SqlParameter("@DOCUMENTNUM",txtDocumentNo.Text.Trim()),
                                        new SqlParameter("@VEHICLENUM",txtVehicle.Text.Trim()),
                                        new SqlParameter("@TRANSPORTNAME",txtTransporter.Text.Trim())                                                                              
                                    };
            DU.executeprocedure("USP_IVR_SaveTranDelivery", sparam);
        }
    }
}

                      